package com.example.myvideo.presenter;


import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Looper;


import com.example.myvideo.contract.VideoPlayerView;

import java.io.IOException;
import java.util.List;

import com.example.myvideo.contract.VideoPlayerView;
import java.util.List;

public class VideoPlayerPresenter {
    private VideoPlayerView view;
    private List<String> videoPaths; // 视频路径列表
    private int currentVideoIndex = 0; // 当前视频索引

    public VideoPlayerPresenter(VideoPlayerView view, List<String> videoPaths) {
        this.view = view;
        this.videoPaths = videoPaths;
    }

    public boolean hasNextVideo() {
        return currentVideoIndex < videoPaths.size() - 1;
    }

    public String getNextVideoPath() {
        if (hasNextVideo()) {
            return videoPaths.get(currentVideoIndex + 1);
        }
        return null;
    }

    public boolean hasPreviousVideo() {
        return currentVideoIndex > 0;
    }

    public String getPreviousVideoPath() {
        if (hasPreviousVideo()) {
            return videoPaths.get(currentVideoIndex - 1);
        }
        return null;
    }

    public void playVideo(int index) {
        if (index < 0 || index >= videoPaths.size()) {
            view.hideLoading();
            return;
        }
        view.showLoading();
        view.playVideo(videoPaths.get(index));
        currentVideoIndex = index;
        view.hideLoading();
    }

    public void playVideo() {
        playVideo(currentVideoIndex);
    }

    public void nextVideo() {
        if (hasNextVideo()) {
            playVideo(currentVideoIndex + 1);
        } else {
            // 可以在这里处理到达列表末尾的情况，例如循环播放
            playVideo(0);
        }
    }

    public void previousVideo() {
        if (hasPreviousVideo()) {
            playVideo(currentVideoIndex - 1);
        } else {
            // 可以在这里处理到达列表开头的情况，例如循环播放
            playVideo(videoPaths.size() - 1);
        }
    }
}